#import "CTInfoCell.h"
#import "NSDate+CTTool.h"
@interface CTInfoCell () <UITextFieldDelegate>
@property (nonatomic ,strong) NSDate *lastDate;
@end
@implementation CTInfoCell
- (void)initialize
{
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.backView];
    [self.backView addSubview:self.contentTF];
    self.arrowView = [[UIImageView alloc] init];
    self.arrowView.image = [UIImage imageNamed:@"arrow_right"];
    [self.backView addSubview:self.arrowView];
    [self.backView addSubview:self.clearBtn];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Ratio(16));
        make.right.mas_equalTo(Ratio(-16));
        make.top.mas_equalTo(Ratio(10));
        make.bottom.mas_equalTo(self.contentView);
    }];
    [self.arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backView);
        make.right.mas_equalTo(Ratio(-12));
        make.size.mas_equalTo(CGSizeMake(Ratio(16), Ratio(16)));
    }];
    [self.clearBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.backView).offset(Ratio(-12));
        make.centerY.equalTo(self.backView);
        make.size.mas_equalTo(CGSizeMake(Ratio(24), Ratio(24)));
    }];
    [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backView).offset(Ratio(12));
        make.centerY.mas_equalTo(self.backView);
        make.right.mas_equalTo(self.arrowView.mas_left).offset(-Ratio(40));
    }];
}
- (void)setCellType:(CTCellType)cellType {
    _cellType = cellType;
    switch (cellType) {
        case CTCellTypeInput:
        case CTCellTypeFirstName:
        case CTCellTypeMiddleName:
        case CTCellTypeLastName:
        case CTCellTypeBVN:
        case CTCellTypeSelectAddress:
        case CTCellTypeSelectAlternateNumber:
        case CTCellTypeCompanyPhone:
        case CTCellTypeSelectEmail:
        {
            self.arrowView.hidden = YES;
            self.contentTF.userInteractionEnabled = YES;
        }
            break;
        case CTCellTypeSex:
        case CTCellTypeBirthday:
        case CTCellTypeEducation:
        case CTCellTypeJobStatus:
        case CTCellTypeProfession:
        case CTCellTypeSalary:
        case CTCellTypeMaritalStatus:
        case CTCellTypeResidence:
        case CTCellTypeHomeStatus:
        case CTCellTypeCity:
        case CTCellTypePayDay:
        case CTCellTypeEntryDay:
        case CTCellTypeRalationShip1:
        case CTCellTypeRalationShip2:
        case CTCellTypeRalationShip3:
        case CTCellTypeContact1:
        case CTCellTypeContact2:
        case CTCellTypeContact3:
        {
            self.clearBtn.hidden = YES;
            self.arrowView.hidden = NO;
            self.contentTF.userInteractionEnabled = NO;
        }
            break;
        default:
            break;
    }
}
- (UIView *)backView
{
    if (!_backView) {
        _backView = [[UIView alloc] init];
        _backView.layer.masksToBounds = YES;
        _backView.layer.cornerRadius = Ratio(12);
        _backView.layer.borderWidth = 1;
        _backView.layer.borderColor = [UIColor colorWithHexString:@"#AFBACA"].CGColor;
        _backView.backgroundColor = [UIColor clearColor];
    }
    return _backView;
}
- (UITextField *)contentTF {
    if (!_contentTF) {
        _contentTF = [[UITextField alloc] init];
        _contentTF.delegate = self;
        _contentTF.font = [UIFont boldSystemFontOfSize:Ratio(14)];
        _contentTF.textColor = [UIColor blackColor];
        @weakify(self);
        [[_contentTF rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x) {
            @strongify(self);            
            self.clearBtn.hidden = (self.contentTF.text.length > 0) ? NO : YES;
            if (self.editingBlock) {
                self.editingBlock(self, self.contentTF.text);
            }
            NSDate *currentDate = [NSDate date];
            NSTimeInterval interval = [NSDate timeIntervalWithStart:self.lastDate end:currentDate];
            NSString *newLog = [NSString stringWithFormat:@"%.2f:%@:0,",interval, self.contentTF.text];
            [self.tfModel.valueParameter appendString:newLog];
            self.lastDate = currentDate;
            NSLog(@"val:-----%@", self.tfModel.valueParameter);
            if (self.cellType == CTCellTypeSelectEmail && self.editBeginBlock) {
                self.editBeginBlock(self, self.contentTF.text);
            }
        }];
        [[_contentTF rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(id x) {
            @strongify(self);
            self.lastDate = [NSDate date];
            if (self.tfModel.dwellDuration == 0) {
                self.tfModel.initiationTimestamp = [self.lastDate timeIntervalSince1970];
            }
            NSString *newLog = [NSString stringWithFormat:@"%d:%@:0,",0,  self.contentTF.text].mutableCopy;
            [self.tfModel.valueParameter appendString:newLog];
        }];
        [[_contentTF rac_signalForControlEvents:UIControlEventEditingDidEnd] subscribeNext:^(id x) {
            @strongify(self);
            NSDate *currentDate = [NSDate date];
            NSTimeInterval interval = [NSDate timeIntervalWithStart:self.lastDate end:currentDate];
            NSString *newLog = [NSString stringWithFormat:@"%f:%@:2,",interval,  self.contentTF.text];
            [self.tfModel.valueParameter appendString:newLog];
            self.lastDate = currentDate;
            self.tfModel.conclusionTimestamp = [currentDate timeIntervalSince1970];
            self.tfModel.dwellDuration = [NSDate timeIntervalWithStartInterval:self.tfModel.initiationTimestamp endInterval:self.tfModel.conclusionTimestamp];
            self.lastDate = currentDate;
            NSLog(@"val:-----%@", self.tfModel.valueParameter);
            if (self.editDidEndBlock) {
                self.editDidEndBlock(self, self.tfModel);
            }
        }];
        [[_contentTF rac_textSignal] subscribeNext:^(NSString* x) {
            @strongify(self);
        }];
    }
    return _contentTF;
}
- (UIButton *)clearBtn
{
    if (_clearBtn == nil) {
        _clearBtn = [[UIButton alloc] init];
        _clearBtn.hidden = YES;
        [_clearBtn setImage:[UIImage imageNamed:@"input_clear"] forState:UIControlStateNormal];
        [_clearBtn setImage:[UIImage imageNamed:@"input_clear"] forState:UIControlStateSelected];
        @weakify(self);
        [[_clearBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            self.contentTF.text = @"";
            self.clearBtn.hidden = YES;
        }];
    }
    return _clearBtn;
}
@end
